package cn.david.user.contronller;

import cn.david.basic.exception.BusinessExcepiton;
import cn.david.basic.util.AjaxResult;
import cn.david.user.dto.LoginDto;
import cn.david.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @PostMapping("/account")
    public AjaxResult  loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.loginAccount(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessExcepiton e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，请稍后再试");
        }

    }
    @PostMapping("/wechat")
    public AjaxResult  wecohatLogin(@RequestBody Map<String,String> param){
        try {
            System.out.println(param);
            return loginService.wecohatLogin(param.get("code"));

        } catch (BusinessExcepiton e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，请稍后再试");
        }

    }

    @PostMapping("/binder/wechat")
    public AjaxResult  wecohatbinder(@RequestBody Map<String,String> param){
        try {
            System.out.println(param);
            Map<String, Object> map = loginService.wecohatbinder(param);
            return AjaxResult.me().setResultObj(map);

        } catch (BusinessExcepiton e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，请稍后再试");
        }

    }
}
